package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.mapper.CrowdMapper;
import cn.wolfcode.trip.base.mapper.FriendMapper;
import cn.wolfcode.trip.base.mapper.GroupMapper;
import cn.wolfcode.trip.base.mapper.TravelMapper;
import cn.wolfcode.trip.base.query.friend.FriendLoopQuery;
import cn.wolfcode.trip.base.service.IFriendService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class FriendServiceImpl implements IFriendService{
    @Autowired
    private FriendMapper friendMapper;
    @Autowired
    private GroupMapper groupMapper;
    @Autowired
    private CrowdMapper crowdMapper;
    @Autowired
    private TravelMapper travelMapper;

    @Override
    public void addFriend(Friend friend) {
        Friend friendBySelect = friendMapper.selectByUserIdAndFriendId(friend);
        if (friendBySelect == null) {
            friend.setCreatdate(new Date());
            Group defaultGroup = getDefaultGroup(friend.getUser().getId());
            friend.setGroup(defaultGroup);
            friendMapper.insert(friend);
            //添加好友是双向的
            friend.setId(friend.getUser().getId());
            friend.getUser().setId(friend.getFriend().getId());
            friend.getFriend().setId(friend.getId());
            Group group = getDefaultGroup(friend.getUser().getId());
            friend.setGroup(group);
            friendMapper.insert(friend);
        } else {
            throw new RuntimeException("对方已是你的好友");
        }
    }

    @Override
    public PageInfo getFriendLoop(FriendLoopQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Travel> list = travelMapper.selectTravelByFriend(qo);
        return new PageInfo(list);
    }

    @Override
    public PageInfo getAttentionFriendLoop(FriendLoopQuery qo) {
        Group group = groupMapper.selectByIdAndName(qo.getId(), "特别关注");
        if(group != null){
            PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
            qo.setGroupId(group.getId());
            List<Travel> list = travelMapper.selectTravelByFriend(qo);
            return new PageInfo(list);
        }
        return null;
    }

    @Override
    public List<Group> getGroupByUserId(Long id) {
        List<Group> list = groupMapper.getGroupByUserId(id);
        return list;
    }

    @Override
    public List<Friend> getFriendByGroupId(Long parentId) {
        List<Friend> list = friendMapper.getFriendByGroupId(parentId);
        return list;
    }

    @Override
    public void addGroup(Group group) {
        groupMapper.insert(group);
    }

    @Override
    public void moveGroup(Long groupId, Long friendId, Long userId) {
        friendMapper.updataGroupByFriendAndUser(groupId,friendId,userId);
    }

    @Override
    public void deleteFriend(Long userId, Long friendId) {
        friendMapper.deleteByUserAndFriend(userId,friendId);
        friendMapper.deleteByUserAndFriend(friendId,userId);
    }

    @Override
    public void addCrowd(Crowd crowd) {
        crowd.setCreatdate(new Date());
        crowdMapper.insert(crowd);
        crowdMapper.inviteCrowd(crowd.getId(),crowd.getUser().getId());
    }

    @Override
    public List<Crowd> getListForCrowds(Long id) {
        List<Crowd> list = crowdMapper.selectByUserId(id);
        return list;
    }

    @Override
    public List<User> getFriendByCrowd(Long id) {
        List<User> list = crowdMapper.selectFriendByCrowdId(id);
        return list;
    }

    @Override
    public Crowd getCrowdById(Long id) {
        Crowd crowd = crowdMapper.selectByPrimaryKey(id);
        return crowd;
    }

    @Override
    public void saveCrowdRelation(Long crowdId, Long userId) {
        Map<String,Object> map = crowdMapper.selectRelation(crowdId,userId);
        if(map == null){
            crowdMapper.inviteCrowd(crowdId,userId);
        }else {
            throw new RuntimeException("你已在该群中");
        }
    }

    @Override
    public void deleteCrowdRelation(Long crowdId, Long userId) {
        crowdMapper.deleteCrowdRelationByCrowdAndUser(crowdId,userId);
    }

    public Group getDefaultGroup(Long userId){
        //查询默认分组
        Group group = groupMapper.selectByIdAndName(userId,"我的好友");
        if(group != null){
            return group;
        }
        //如果不存在则创建两个默认分组
        User user = new User();
        user.setId(userId);
        Group defaultGroup = new Group();
        defaultGroup.setUser(user);
        defaultGroup.setName("我的好友");
        Group attentionGroup = new Group();
        attentionGroup.setUser(user);
        attentionGroup.setName("特别关注");
        groupMapper.insert(attentionGroup);
        groupMapper.insert(defaultGroup);
        return defaultGroup;
    }
}
